package team.seekers.skill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.skill.entity.dao.UserSkill;
import team.seekers.skill.entity.dao.StatisticsDaily;
import team.seekers.skill.entity.dao.User;
import team.seekers.skill.mapper.StatisticsDailyMapper;
import team.seekers.skill.service.UserSkillService;
import team.seekers.skill.service.StatisticsDailyService;
import team.seekers.skill.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author cbj
 * @since 2021-02-08
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UserService userService;
    @Autowired
    private UserSkillService userSkillService;
    @Override
    public boolean statisticData(String day) {
        //为了保持数据库中某一个日期的唯一性，添加之前先删除一下
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.eq("create_date",day);
        QueryWrapper<User> userWrapper1 = new QueryWrapper<>();
        userWrapper1.eq("last_login_date",day);
        //得到指定日期的注册人数
        int RegisterCount = userService.count(userWrapper);
        //得到某个日期的所有技能的正在学习的人数
        QueryWrapper<UserSkill> skillUserWrapper = new QueryWrapper<>();
        skillUserWrapper.eq("status",1);
        int LearningCount = userSkillService.count(skillUserWrapper);
        //得到某个日期的所有技能的已经学完的人数
        skillUserWrapper.eq("status",2);
        int completeCount = userSkillService.count(skillUserWrapper);
        //得到某个日期的登录人数
        int loginCount = userService.count(userWrapper1);
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(RegisterCount);
        statisticsDaily.setSkillLearnNum(LearningCount);
        statisticsDaily.setSkillCompleteNum(completeCount);
        //TODO 真实的登录数目
        statisticsDaily.setLoginNum(loginCount);
        int insert = baseMapper.insert(statisticsDaily);
        if(insert == 0)
            return false;
        return true;

    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        //只查询两列值，提高查询的效率
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            //封装日期list集合
            date_calculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "skill_learn_num":
                    numDataList.add(daily.getSkillLearnNum());
                    break;
                case "skill_complete_num":
                    numDataList.add(daily.getSkillCompleteNum());
                    break;
                default:
                    break;
            }
        }

        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;

    }
}
